﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using PIBV;

namespace MTViewer
{
    /// <summary>
    /// Interaction logic for ctrlCauseEffect.xaml
    /// </summary>
    public partial class ctrlCauseEffect : UserControl, ITouchObject
    {
        public ctrlCauseEffect()
        {
            InitializeComponent();
            DataContext = this;
        }

        public string ImagePath
        {
            get { return (string)GetValue(ImagePathProperty); }
            set { SetValue(ImagePathProperty, value); }
        }

        public double Angle
        {
            get { return (double)GetValue(AngleProperty); }
            set { SetValue(AngleProperty, value); }
        }

        public double ScaleX
        {
            get { return (double)GetValue(ScaleXProperty); }
            set { SetValue(ScaleXProperty, value); }
        }

        public double ScaleY
        {
            get { return (double)GetValue(ScaleYProperty); }
            set { SetValue(ScaleYProperty, value); }
        }

        public double X
        {
            get { return (double)GetValue(XProperty); }
            set { SetValue(XProperty, value); }
        }

        public double Y
        {
            get { return (double)GetValue(YProperty); }
            set { SetValue(YProperty, value); }
        }

        public string PdfFileName
        {
            get { return (string)GetValue(PdfFileNameProperty); }
            set { SetValue(PdfFileNameProperty, value); }
        }

        public string DgnFileName
        {
            get { return (string)GetValue(DgnFileNameProperty); }
            set { SetValue(DgnFileNameProperty, value); }
        }

        public string OriginalImagePath
        {
            get { return (string)GetValue(OriginalImagePathProperty); }
            set { SetValue(OriginalImagePathProperty, value); }
        }

        public event EventHandler<PIBV.LoadDrawingEventArgs> OnCreateControl;

        #region DependencyProperties

        // Using a DependencyProperty as the backing store for ImagePath.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ImagePathProperty =
            DependencyProperty.Register("ImagePath", typeof(string), typeof(ctrlCauseEffect), new UIPropertyMetadata(""));

        // Using a DependencyProperty as the backing store for Angle.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty AngleProperty =
            DependencyProperty.Register("Angle", typeof(double), typeof(ctrlCauseEffect), new UIPropertyMetadata(0.0));

        // Using a DependencyProperty as the backing store for ScaleX.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ScaleXProperty =
            DependencyProperty.Register("ScaleX", typeof(double), typeof(ctrlCauseEffect), new UIPropertyMetadata(1.0));

        // Using a DependencyProperty as the backing store for ScaleY.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ScaleYProperty =
            DependencyProperty.Register("ScaleY", typeof(double), typeof(ctrlCauseEffect), new UIPropertyMetadata(1.0));

        // Using a DependencyProperty as the backing store for X.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty XProperty =
            DependencyProperty.Register("X", typeof(double), typeof(ctrlCauseEffect), new UIPropertyMetadata(0.0));

        // Using a DependencyProperty as the backing store for Y.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty YProperty =
            DependencyProperty.Register("Y", typeof(double), typeof(ctrlCauseEffect), new UIPropertyMetadata(0.0));

        // Using a DependencyProperty as the backing store for ImagePath.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PdfFileNameProperty =
            DependencyProperty.Register("PdfFileName", typeof(string), typeof(ctrlCauseEffect), new UIPropertyMetadata(""));

        // Using a DependencyProperty as the backing store for ImagePath.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty OriginalImagePathProperty =
            DependencyProperty.Register("OriginalImagePath", typeof(string), typeof(ctrlCauseEffect), new UIPropertyMetadata(""));

        // Using a DependencyProperty as the backing store for ImagePath.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DgnFileNameProperty =
            DependencyProperty.Register("DgnFileName", typeof(string), typeof(ctrlCauseEffect), new UIPropertyMetadata(""));

        #endregion

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            FillData();
        }

        /// <summary>
        /// Get data from the WebService
        /// </summary>
        void FillData()
        {
            PID_CauseEffect ce = App.DbManager.CreateObject<PID_CauseEffect>();
            CauseEffect[] arr = ce.WrapDataTable(ce.FillData("*" + ImagePath.Replace(".cef", "") + "*"));
            lstCE.ItemsSource = arr;

            //try
            //{
            //    ServiceReference1.SasLogicClient s = new ServiceReference1.SasLogicClient();
            //    s.Open();
            //    ServiceReference1.CauseEffect[] recs = s.GetCauseEffect("*"+ImagePath.Replace(".cef", "")+"*");
            //    lstCE.ItemsSource = recs;
            //    s.Close();
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message, "WebService error", MessageBoxButton.OK, MessageBoxImage.Error);
            //}
        }

        private void btnClose_TouchDown(object sender, TouchEventArgs e)
        {
            TouchObjectFactory.RemoveTouchObjectFromCanvas(this as ITouchObject);
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            TouchObjectFactory.RemoveTouchObjectFromCanvas(this as ITouchObject);
        }

    }
}
